package suanfarumen;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class day11_2 {
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> target=new ArrayList<>();
        Deque<Integer> stack = new ArrayDeque<>();
        boolean[] used=new boolean[nums.length];
        dfs(nums,stack,target,used);
        return target;
    }
    public static void dfs(int[] nums, Deque<Integer> stack, List<List<Integer>> target, boolean[] used){
        if(stack.size()==nums.length){
            target.add(new ArrayList<>(stack));
            return;
        }
        for(int i=0;i<nums.length;++i){
            if (used[i]){
                continue;
            }
            used[i]=true;
            stack.addLast(nums[i]);
            dfs(nums,stack,target, used);
            stack.removeLast();
            used[i]=false;
        }
    }

    public static void main(String[] args) {
        System.out.println(permute(new int[]{1,2,3}));
    }
}
